package repository

import (
	models "django_blog_gin/internal/model"
	"django_blog_gin/internal/svc"
	"fmt"
)

type Account struct {
	svcCtx *svc.ServiceContext
}

func NewAccount(svcCtx *svc.ServiceContext) *Account {
	return &Account{svcCtx: svcCtx}
}

func (i *Account) IdGetUser(id int) (models.AccountsBlogUser, error) {
	userModel := models.AccountsBlogUser{}
	sql := fmt.Sprintf("SELECT * FROM %s WHERE id = %d", userModel.TableName(), id)
	err := i.svcCtx.MysqlDb.Get(&userModel, sql)
	if err != nil {
		return userModel, err
	}
	return userModel, nil
}

func (i *Account) UsernameGetUser(username string) (models.AccountsBlogUser, error) {
	userModel := models.AccountsBlogUser{}
	sql := fmt.Sprintf("SELECT * FROM %s WHERE username = '%s'", userModel.TableName(), username)
	err := i.svcCtx.MysqlDb.Get(&userModel, sql)
	if err != nil {
		return userModel, err
	}
	return userModel, nil
}
